/*
 * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef BASE_OHOS_SLIDING_OBSERVER_H_
#define BASE_OHOS_SLIDING_OBSERVER_H_

#include <stdint.h>

#include <vector>

#include "base/ohos/ltpo/include/ltpo_strategy.h"
#include "third_party/ohos_ndk/includes/ohos_adapter/adapter_base.h"
namespace base {
namespace ohos {
// dynamic frame rate
class SlidingObserver {
 public:
  SlidingObserver();
  ~SlidingObserver();

  SlidingObserver(const SlidingObserver&) = delete;
  SlidingObserver& operator=(const SlidingObserver&) = delete;

  static SlidingObserver& GetInstance();

  void StartSliding();
  int32_t StopSliding();
  void StartFling();
  int32_t StopFling();
  int32_t OnScrollUpdate(float delta_x, float delta_y);
  int32_t OnFlingUpdate(float velocity_x, float velocity_y);
  void OnDisplayInfoChange();
#if BUILDFLAG(ARKWEB_PDF)
  void SetIsPdf(bool is_pdf);
  bool IsPdf();
#endif

 private:
  float GetVelocity(float velocity_x, float velocity_y);
  int32_t GetPreferedFrameRate(
      float velocity,
      const std::vector<OHOS::NWeb::FrameRateSetting>& setting);
  int64_t GetCurrentTimestamp();
  void UpdateFrameRateForPC();

 private:
  bool is_inited_{false};
  bool is_sliding_{false};
  bool is_off_screen_{false};
#if BUILDFLAG(ARKWEB_PDF)
  bool is_pdf_{false};
  bool use_pdf_rate_{false};
#endif
  int32_t dpi_{-1};
  std::vector<OHOS::NWeb::FrameRateSetting> on_screen_setting_{};
  std::vector<OHOS::NWeb::FrameRateSetting> off_screen_setting_{};

  float virtual_pixel_ratio_{-1};
  int64_t current_timestamp_{-1};
  int32_t sliding_frame_rate_{0};

  bool is_web_list_fling_{false};
  LTPOStrategy strategy_{LTPOStrategy::DISABLED};
};
}  // namespace ohos
}  // namespace base

#endif  // BASE_OHOS_SLIDING_OBSERVER_H_
